package DatabaseManager;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import Other.*;
public class UniverseTable extends Table {

	/**
	 * The function builds a table object (doesn't actually create it in the
	 * database)
	 * 
	 * @param db_mgr
	 *            - the database manager
	 * @throws SQLException
	 *             - thrown in case of an sql exception
	 * @throws ClassNotFoundException
	 *             - thrown in case of a class not found exception
	 */

	public UniverseTable(DBManager db_mgr) throws SQLException,
			ClassNotFoundException {
		super(db_mgr);

		table_name = "fictional_universe";
		primaryKey = "universe_id";
		String[] tmp = { "name", "universe_id" };
		columns = tmp;

		foreignKey = new String[columns.length];
		for (int i = 0; i < foreignKey.length; i++)
			foreignKey[i] = null;

		String[] tmp1= {"VARCHAR(80)", "INT"};
		dataTypes = tmp1;

		boolean[] tmp2 = { false, false };
		isDataTypeNull = tmp2;
		isMyISAM = true;
	}

	/**
	 * 
	 * @param pageNumber
	 *            - number of page
	 * @param entriesPerPage
	 *            - rows per page
	 * @return names of all universes in the page
	 * @throws SQLException
	 */
	public List<Universe> getUniversePage(int pageNumber, int entriesPerPage)
			throws SQLException {
		ResultSet rs = super.getPage(pageNumber, entriesPerPage, "name", "*", null, null);
		List<Universe> universes = new ArrayList<Universe>();
		try {
			while (rs.next()) {
				universes.add(new UniverseConcrete(rs.getString(1), rs
						.getInt(2)));
				// mechanism to ensure closing
			}
		} catch (SQLException e) {
			db_mgr.closeLastStatement();
			throw e;
		}
		db_mgr.closeLastStatement();
		return universes;
	}
}
